//
//  BSSRootViewController.m
//  KindergartenDemo
//
//  Created by 李传铎 on 2020/11/7.
//  Copyright © 2020 李传铎. All rights reserved.
//

#import "BSSRootViewController.h"
#import "HomeViewController.h"
#import "OrderManagementViewController.h"
#import "MyViewController.h"
#import "MCTabBar.h"
@interface BSSRootViewController ()<UITabBarControllerDelegate>
@property (nonatomic, strong) MCTabBar *mcTabbar;
@end

@implementation BSSRootViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.tabBar.tintColor = [UIColor colorFromHexCode:@"#D7A3A4"];
    if(IOS10_OR_LATER) {
        [self.tabBar setUnselectedItemTintColor:[UIColor colorFromHexCode:@"#D7A3A4"]];
    }
    
    _mcTabbar = [[MCTabBar alloc] init];
    [_mcTabbar.centerBtn addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
    //选中时的颜色
    _mcTabbar.tintColor = [UIColor blueColor];
    //透明设置为NO，显示白色，view的高度到tabbar顶部截止，YES的话到底部
    _mcTabbar.translucent = NO;
    _mcTabbar.frame = CGRectMake(0, SCREEN_HEIGHT-__kTabBarHeight__, SCREEN_WIDTH, __kTabBarHeight__);
    //去掉tabBar顶部线条
    //    CGRect rect = CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
    //    UIGraphicsBeginImageContext(rect.size);
    //    CGContextRef context = UIGraphicsGetCurrentContext();
    //    CGContextSetFillColorWithColor(context, [[UIColor clearColor] CGColor]);
    //    CGContextFillRect(context, rect);
    //    UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
    //    UIGraphicsEndImageContext();
    //    [_mcTabbar setBackgroundImage:img];
    //    [_mcTabbar setShadowImage:img];
    //利用KVC 将自己的tabbar赋给系统tabBar
    //[self setValue:_mcTabbar forKeyPath:@"tabBar"];
    
    UIView *backView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, __kTabBarHeight__)];
    backView.backgroundColor = COLORRGB(255, 255, 255);
    [self.tabBar insertSubview:backView atIndex:0];// 颜色
    self.tabBar.opaque = YES;
    
    self.delegate = self;
    [self setupChildControllers];
    
    // Do any additional setup after loading the view.
}

- (void)buttonAction:(UIButton *)button{
    self.selectedIndex = 2;//关联中间按钮
    button.selected = YES;
}

// tabbar选择时的代理
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController{
    if (tabBarController.selectedIndex == 2){
        //选中中间的按钮
        _mcTabbar.centerBtn.selected = YES;
    }else {
        _mcTabbar.centerBtn.selected = NO;
    }
}

- (void)setupChildControllers {
    
    
    
    // 首页
    HomeViewController * sotre = [[HomeViewController alloc] init];
    
    UINavigationController * sotreNavi   = [[UINavigationController alloc] initWithRootViewController:sotre];
    [self setupChildViewController:sotreNavi tabBarImageName:@"home_noSelect" rootViewControllerTitle:@"首页"];
    
    // 订单管理
    OrderManagementViewController * Order = [[OrderManagementViewController alloc] init];
    
    UINavigationController * OrderNavi   = [[UINavigationController alloc] initWithRootViewController:Order];
    [self setupChildViewController:OrderNavi tabBarImageName:@"statistics_noSelect" rootViewControllerTitle:@"订单管理"];
    
    // 我的
    MyViewController * My = [[MyViewController alloc] init];
    
    UINavigationController * MyNavi   = [[UINavigationController alloc] initWithRootViewController:My];
    [self setupChildViewController:MyNavi tabBarImageName:@"my_noSelect" rootViewControllerTitle:@"我的"];
}

- (void)setupChildViewController:(UIViewController *)childVC
                 tabBarImageName:(NSString *)name
         rootViewControllerTitle:(NSString *)title{
    childVC.title = title;
    
    childVC.tabBarItem.image = OrigIMG(name);
    
    NSString *selectedImage = [name stringByReplacingOccurrencesOfString:@"noSelect" withString:@"Select"];
    childVC.tabBarItem.selectedImage = OrigIMG(selectedImage);
    
    [childVC.tabBarItem setTitleTextAttributes:@{NSForegroundColorAttributeName: [UIColor colorFromHexCode:@"#999999"],NSFontAttributeName:[UIFont boldSystemFontOfSize:13]} forState:UIControlStateNormal];
    [childVC.tabBarItem setTitleTextAttributes:@{NSForegroundColorAttributeName: [UIColor colorFromHexCode:@"#F34B4D"],NSFontAttributeName:[UIFont boldSystemFontOfSize:13]} forState:UIControlStateSelected];
    [self addChildViewController:childVC];
}

#warning -----未登录时不可支付

- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController{
    
    return YES;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



@end
